/**
 * Created by DevilsEyes on 2016/4/22.
 */
'use strict';

avalon.config({
    baseUrl: "/admin/",
    paths: {
        jQuery: '//cdn.bootcss.com/jquery/2.2.1/jquery.js',
        // avalon: '//cdn.bootcss.com/avalon.js/1.5.5/avalon.modern.min.js',
        avalon: '//cdn.bootcss.com/avalon.js/1.4.7.2/avalon.modern.min.js',

        //由于来自东方的神秘力量，这个貌似支持amd的库并不能amd加载。
        preview: 'js/vendor/blueimp-load-image.min.js',

        upload: '//cdn.bootcss.com/plupload/2.1.8/plupload.full.min.js',

        layer: 'js/vendor/layer/layer.js',
        ex: 'js/vendor/ex.js',

        md5: 'js/md5.min.js',
        router: 'js/avalon/mmRouter.js'
    },
    priority: ['/js/avalon/text.js', '/js/avalon/css.js'],
    shim: {
        upload: {
            exports: 'plupload'
        },
        preview: {
            exports: 'loadImage'
        }
        // layer: ["css!vendor/avalon/skin/layer.css"]
    }
});

require(['layer', 'ex'], function (layer) {
    layer.config({
        extend: 'js/vendor/layer/extend/layer.ext.js'
    });
});

var PAGE = {
    login: '/page/login/login.js',

    homeworkAdd: '/page/homework/add/add.js',
    homeworkView: '/page/homework/view/view.js',
    homeworkList: '/page/homework/list/list.js',

    courseDetail: '/page/course/detail/detail.js',
    courseList: '/page/course/list/list.js',

    noticeDetail: '/page/notice/detail/detail.js',
    noticeList: '/page/notice/list/list.js',

    studentVerify: '/page/student/verify/verify.js',
    studentList: '/page/student/list/list.js'
};

var PAGE_TEMPLATE = {
    404: './page/404/404.html'
};

window.$root = avalon.define({
    $id: 'root',
    login: false,
    page: PAGE_TEMPLATE[404],
    logout: function () {
        $.ajax({
            url: '/api/admin/user/logout/',
            method: 'GET',
            success: function (obj) {
                if (obj.code == 0) {
                    $root.login = false;
                    location.hash = '#!/login/';
                } else {
                    //todo tips
                }
            }
        });
    }
});

$.ajax({
    url: '/api/admin/user/login/',
    method: 'GET',
    success: function (obj) {
        if (!obj.user) {
            location.hash = '#!/login/';
        } else {
            if (location.hash == '#!/login/') {
                location.hash = '/student/list'
            }
            $root.login = true;
        }
        RouterStart();
    }
});

var RouterStart = function () {
    require(['router'], function () {

        $(function () {
            avalon.scan();

            var RouterMap = {

                '/login': 'login',

                '/homework/list': 'homeworkList',
                '/homework/add': 'homeworkAdd@add',
                '/homework/edit/:id': 'homeworkAdd@edit',
                '/homework/view/:id': 'homeworkView',

                '/course/list': 'courseList',
                '/course/add': 'courseDetail@add',
                '/course/edit/:id': 'courseDetail@edit',

                '/notice/list': 'noticeList',
                '/notice/add': 'noticeDetail@add',
                '/notice/edit/:id': 'noticeDetail@edit',

                '/student/list': 'studentList',
                '/student/verify': 'studentVerify'

            };
            for (var path in RouterMap) {
                var controller = RouterMap[path].split('@')[0];
                var method = RouterMap[path].split('@')[1] || 'init';

                avalon.router.get(path, (function (controller, method) {
                    return function (Rp) {
                        require([PAGE[controller]], function (p) {
                            $root.page = './' + PAGE[controller].replace('.js', '.html');
                            p[method](Rp);
                        })
                    }
                })(controller, method));
            }

            avalon.router.error(function () {
                if (location.hash == '#!/') {
                    location.hash = '#!/login/';
                } else {
                    $root.page = PAGE_TEMPLATE[404];
                }
            });

            avalon.history.start();
        })

    });
};
